import { _decorator, Button, Component, Node, Sprite } from 'cc';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UIExitAnimtions } from '../../Init/Tools/UIExitAnimations';
const { ccclass, property } = _decorator;

// 头条专属 侧边栏
@ccclass('NavigateToSceneUI')
export class NavigateToSceneUI extends BasicUI {

    @property(Node)
    bgMask: Node = null;
    @property(Node)
    panel: Node = null;

    @property(Node)
    awardBtn: Node = null;

    isCanGetAward = false;

    // 是否从侧边栏进入
    isFromSideBar = false;

    protected onLoad(): void {
        super.onLoad();
        EventManager.emit(EventTypes.SDKEvents.SetOnShowCallBack, this.onSetOnShowCallBack.bind(this));
    }

    public show(d?: any): void {
        super.show();
        this.checkInfo();
    }

    protected checkInfo(): void {
        // 检测 是否显示
        this.awardBtn.active = true;

        // 获取场景值
        EventManager.emit(EventTypes.SDKEvents.GetOnShowMsg, (res) => {
            // 启动场景字段
            this.isFromSideBar = res && res.scene == '021036';
        })

        const navigateToScene: { isFinish: boolean, lastTime: number } = StorageSystem.getData().userAssets.dayInfo.navigateToScene;

        // 判断上次领取时间距离今天是否过了一天
        const curDay = new Date(navigateToScene.lastTime);
        const lastDay = new Date();
        const _isChecked =
            curDay.getFullYear() == lastDay.getFullYear() &&
            curDay.getMonth() == lastDay.getMonth() &&
            curDay.getDate() == lastDay.getDate();

        if (!_isChecked) {
            navigateToScene.isFinish = false;
            StorageSystem.saveData();
        }

        // 是否今天已领取
        this.isCanGetAward = this.isFromSideBar && !navigateToScene.isFinish;
        this.setAwardInfo();
    }

    showHideAnim() {
        this.bgMask.getComponent(UIExitAnimtions).showHideAnim(false);
        this.panel.getComponent(UIExitAnimtions).showHideAnim(false, () => {
            UISystem.hideUI(UIEnum.NavigateToSceneUI);
        }, true);
    }

    setAwardInfo() {
        const navigateToScene: { isFinish: boolean, lastTime: number } = StorageSystem.getData().userAssets.dayInfo.navigateToScene;

        // 奖励按钮 文字显示
        let awardSp = this.awardBtn.getComponent(Sprite);
        let finishLabel = this.awardBtn.getChildByName('finishLabel');
        let awardLabel = this.awardBtn.getChildByName('awardLabel');

        awardSp.grayscale = navigateToScene.isFinish;

        finishLabel.active = navigateToScene.isFinish;
        awardLabel.active = !navigateToScene.isFinish;
    }
    // 回到前台时的回调 启动游戏时不会触发
    onSetOnShowCallBack(res) {
        this.isFromSideBar = res.scene == '021036';
        this.checkInfo();
        console.log('# onSetOnShowCallBack', res.scene);
    }

    onNavigateToSceneClick() {
        EventManager.emit(EventTypes.SDKEvents.NavigateToScene);
    }

    onGetAwardClick() {
        const userAssets = StorageSystem.getData().userAssets;
        if (this.isCanGetAward) {

            userAssets.dayInfo.navigateToScene.isFinish = true;
            userAssets.dayInfo.navigateToScene.lastTime = new Date().getTime();
            // 奖励
            userAssets.asset += 100;    // 每天获得100金币
            userAssets.energy += 50;    // 每天获得50能量

            StorageSystem.saveData();
            StorageSystem.updateToAssets(true);

            this.isCanGetAward = false;
            this.setAwardInfo();

        } else {
            if (!this.isFromSideBar) {
                EventManager.emit(EventTypes.GameEvents.ShowTips, '点击导航侧边栏按钮，再进入游戏即可领取奖励');
            }
            if (userAssets.dayInfo.navigateToScene.isFinish) {
                EventManager.emit(EventTypes.GameEvents.ShowTips, '今天已领取奖励');
            }
        }

    }

    onCloseBtnClick() {
        this.showHideAnim();
    }
}

